home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Now 11 / CD-ROM Now MegaDisc 11 (1995-02).iso / discs / tcl / tcl_faq1.par < prev    next >
Encoding:
Text File  |  1994-10-27  |  57.3 KB  |  1,717 lines

  1. Newsgroups: comp.lang.tcl,comp.answers,news.answers
  2. Path: lvirden
  3. From: lwv26@cas.org (Larry W. Virden)
  4. Subject: FAQ: comp.lang.tcl Frequently Asked Questions (2/5)
  5.     (Last updated: March 22, 1994)
  6. Followup-To: comp.lang.tcl
  7. Summary: A regular posting of the comp.lang.tcl Frequently Asked Questions 
  8.     (FAQ) and their answers.  This is the second of five parts.
  9.     This part covers the how-to questions and answers.
  10. Originator: lvirden@cas.org
  11. Keywords: tcl, expect, extended tcl, wish, tk
  12. Sender: lvirden@cas.org
  13. Reply-To: lvirden@cas.org (Larry W. Virden)
  14. Approved: news-answers-request@MIT.Edu
  15.  
  16. Archive-name: tcl-faq/part2
  17. Version: 4.7
  18. Last-modified: March 22, 1994
  19.  
  20. Please do not be offended if I neglect to acknowledge your contribution
  21. to this FAQ!  I sometimes forget to put an attribution in.  I especially
  22. try to add them if it appears that there is further experimentation or
  23. debugging being done though.
  24.  
  25.     For more information concerning Tcl (see "tcl-faq/part1"),
  26. (see "tcl-faq/part3"), (see "tcl-faq/part4"), or (see "tcl-faq/part5").
  27.  
  28. Index of questions:
  29.  
  30. VIII. Questions on building Tcl and friends on your system:
  31.     Q8A. Is there anywhere I can find help with the details of getting 
  32.         Tcl to work on my machine?
  33.     Q8B. Why does the link step says that some of the functions Tcl,
  34.         needs are missing when I am trying to build it?
  35.     Q8C. Has anyone built Tcl 6 on an RS/6000 AIX 3.1? 
  36.     Q8D. Has anyone gotten Tcl to compile under HP-UX?
  37.     Q8E. Has anyone gotten Tcl to compile under VMS?
  38.     Q8F. What does it take to get Tcl to compile under SCO Unix?
  39.     Q8G. Why do I get format and scan errors when I run tclTest
  40.         (NeXT, AIX, etc.)?
  41.     Q8H. Why do I get lots of errors under Irix 4.0.1 when I run
  42.         tclTest?
  43.     Q8I. Does anyone else have problems with Tcl on a Cray?
  44.     Q8J. Does anyone know how to get Tk to run on a SparcBook or other
  45.         laptop with a limited number of colors?
  46.     Q8K. What does it take to get Tcl/Tk to compile on 386bsd/Linux 
  47.         or other POSIX/ANSI C systems not already supported?
  48.     Q8L. Why do I get a parse error in tkInit.c when I try to compile
  49.         Tk on my Irix machine?
  50.     Q8M. What do I need to do to install Tcl 7.x/Tk 3.x on NeXTSTEP 3.1?
  51.     Q8N. Why can't I print the draft of the Tcl/Tk book?
  52.     Q8O. Why am I getting errors on my SGI Indigo workstation?
  53.     Q8P. How do I build expect on Solaris 2.3?
  54.  
  55. IX.  How, in Tcl, can I XXX:
  56.     Q9A. get association lists or property lists?
  57.     Q9B. call one proc with the multi parameter value returned by 
  58.         another proc?
  59.     Q9C. pass an array into a proc?
  60.     Q9D. pipe output of a command back into a Tcl parsing procedure?
  61.     Q9E. merge extended Tcl into other programs such as wish or expect?
  62.     Q9F. delete a procedure from within a script?
  63.     Q9G. get parray to recognize an array variable I have created
  64.         via upvar?
  65.     Q9H. get more than 7 digits of double precision?
  66.     Q9I. grab the command line whenever a non-built-in call is made?
  67.     Q9J. get or set an environment variable?
  68.     Q9K. use numbers with leading zeroes?
  69.     Q9L. find the command line arguments to my application?
  70.     Q9M. put comments in my script, for example in a case statement?
  71.     Q9N. redirect stdin or stdout safely, including binary data?
  72.     Q9O. trap signals, and other more Unix specific functions?
  73.     Q9P. get quoted strings to work the way I want?
  74.     Q9Q. share procedures between multiple tcl applications?
  75.     Q9R. get items inserted into a list?
  76.     Q9S. perform a non-blocking read on a file identifier?
  77.     Q9T. read and write from a pipe without locking up?
  78.  
  79. X.  How, in Tk, can I XXX:
  80.     A. Question on starting Tk applications
  81.  
  82.     Q10.A.1. get my wish application to execute - I just get a wish prompt!
  83.     Q10.A.2. ,using a machine with less than 8 bit color, run?
  84.     Q10.A.3. set X11 resources for a wish application in an 
  85.         app-defaults file?
  86.     Q10.A.4. specify bitmap patterns on the command line instead of 
  87.         as a file name?
  88.     Q10.A.5. get a Motif user interface?
  89.     Q10.A.6. get an OpenLook user interface?
  90.     Q10.A.7. get Tk 3.3 to even start - I get security error messages.
  91.  
  92.     B. Questions on Tk applications and the keyboard
  93.  
  94.     Q10.B.1. change the default class bindings?
  95.     Q10.B.2. delete a binding?
  96.     Q10.B.3. change a binding while it is being executed?
  97.     Q10.B.4. bind the arrow key on my Sun keyboard?
  98.     Q10.B.5. get root's mouse bindings to work in my Tk application?
  99.  
  100.     C. Questions on Tk and X11 interactions
  101.  
  102.     Q10.C.1. get an application to also use libXt?
  103.     Q10.C.2. change the X11 cursor?
  104.     Q10.C.3. raise or lower a window?
  105.     Q10.C.4. re-map a withdrawn window id?
  106.     Q10.C.5. use Tk in a subwindow of a non-Tk X11 application?
  107.     Q10.C.6. bind <Enter> and <Leave> to a frame containing other widgets?
  108.     Q10.C.7. mix interactions between Xt and Tk/Tcl?
  109.  
  110.     D. Questions on Tk listboxes
  111.  
  112.     Q10.D.1. resize a listbox?
  113.     Q10.D.2. select two items that are not adjacent in the listbox at 
  114.         one time?
  115.     Q10.D.3. select items in more than one Tk listbox at a time?
  116.     Q10.D.4. avoid fractional white space at the end of a resizable listbox?
  117.  
  118.     E. Questions on Tk canvases
  119.  
  120.     Q10.E.1. get output from a Tk canvas?
  121.     Q10.E.2. fill a canvas which is bounded by lines as opposed to a
  122.         shape like a polygon, oval, etc.?
  123.     Q10.E.3. raise/lower canvas window objects or draw graphics onto a 
  124.         window object inside a canvas?
  125.     Q10.E.4. detect when the canvas has been resized?
  126.  
  127.     F. Other questions
  128.  
  129.     Q10.F.1. get the name of my own interpreter?
  130.     Q10.F.2. get -relief to work on my text widgets?
  131.     Q10.F.3. create a scrollable window of buttons?
  132.     Q10.F.4. pack a text widget so that it can be resized interactively?
  133.     Q10.F.5. create a widget with an upper case name?
  134.     Q10.F.6. create equal sized buttons?
  135.     Q10.F.7. vertically stack radio buttons aligning regardless of font?
  136.     Q10.F.8. initialize an entry widget with some text?
  137.  
  138. End of FAQ Index
  139.  
  140. ----------------------------------------------------------------------
  141.  
  142. ------------------------------
  143.  
  144. From: -VIII- Questions on building Tcl and friends on your system
  145. Subject: -Q8A- Is there anywhere I can find help with the details of 
  146.         getting Tcl to work on my machine?
  147.  
  148. A8A. Glad you asked!  Look in the Tcl distribution for the file called
  149. "porting.notes".  This will contain a collection of notes that various people
  150. have provided about porting Tcl to various machines and operating systems.
  151. There are also a file called "README" which should be read FIRST - before
  152. doing anything else with the code (this should always be one's first
  153. step with any package).  Finally, there is a "changes" file which details
  154. what has changed since the last release - be sure to read this to see
  155. what might need to change in your programs.
  156.  
  157. ------------------------------
  158.  
  159. From: -VIII- Questions on building Tcl and friends on your system
  160. Subject: -Q8B- Why does the link step says that some of the functions Tcl,
  161.         needs are missing when I am trying to build it?
  162.  
  163. A8B. Did you run the "config" program first, by doing a "csh ./config" or
  164. equivalent?  Without doing this, things such as strtoul or strerror
  165. are sometimes mentioned as missing.
  166.  
  167. Tcl includes equivalents for at least the following functions and include files
  168. which may not be found on some systems:
  169.  
  170. dirent.h       limits.h          stdlib.h         string.h
  171.  
  172. opendir.c      strerror.c     strstr.c         strtol.c        strtoul.c
  173.  
  174. strtod.c
  175.  
  176. ------------------------------
  177.  
  178. From: -VIII- Questions on building Tcl and friends on your system
  179. Subject: -Q8C- Has anyone built Tcl 6 on an RS/6000 AIX 3.1? 
  180.  
  181. A8C. See porting notes - especially the note about strtoul.
  182.  
  183. One user got Tcl to compile with a few minor source modifications
  184. (for example, duplicate case statements for errno and signal symbols in 
  185. tclUnixStr.c). 
  186.  
  187. A few other problems arose in the tests.  One is caused by AIX printf
  188. not formatting %#x and %#o correctly when the value to be printed is
  189. zero:  they print "0x0" and "00" instead of "0" and "0" respectively.
  190. This was reported as not a problem in earlier releases.  No fixes have
  191. been posted.
  192.  
  193. Finally, a problem occurs in open.test.  test 13.6 hangs because "cat"
  194. on the RS6000 is unbuffered.  A workaround is to change the execution
  195. of "cat" in open.test to do a "cat -u".
  196.  
  197. Dov Grobgeld <dov@menora.weizmann.ac.il> provided info on creating Tcl
  198. and Tk shared libraries under AIX 3.1.5:
  199.  
  200. For Tcl:
  201.  
  202. cc -o tkshar.o *.o -bE:tclshar.exp -bM:SRE -berok -lX11 -lm
  203. ar r libtclshr tclshar.o
  204.  
  205. For Tk:
  206.  
  207. cc -o tkshar.o *.o -bE:tkshar.exp -bM:SRE -berok -Ltcl -lX11 -lm -ltclshr
  208. ar r libtkshr tkshar.o
  209.  
  210. where tckshar.exp and tkshar.exp had lists of the external functions.
  211.  
  212. There are also problems with fonts on AIX and the IBM.  A patch is needed
  213. from IBM to fix the X server so that fonts are working
  214.  
  215. ------------------------------
  216.  
  217. From: -VIII- Questions on building Tcl and friends on your system
  218. Subject: -Q8D- Has anyone gotten Tcl to compile under HP-UX?
  219.  
  220. A8D. See the porting notes.
  221.  
  222. ------------------------------
  223.  
  224. From: -VIII- Questions on building Tcl and friends on your system
  225. Subject: -Q8E- Has anyone gotten Tcl to compile under VMS?
  226.  
  227. A8E. Information from jkimball@src.honeywell.com (John Kimball) on
  228. May 4, 1993 was that he had gotten Tcl 6.7 and Tk 3.2 ported to VMS
  229. 5.5.  See the catalog for the file information.
  230.  
  231. ------------------------------
  232.  
  233. From: -VIII- Questions on building Tcl and friends on your system
  234. Subject: -Q8F- What does it take to get Tcl to compile under SCO Unix?
  235.  
  236. A8F. Add a "#undef select" to tkEvent.c, and remove the reference to 
  237. TK_EXCEPTION around line 460 of main.c.
  238.  
  239. Tk uses its own scheme for allocating the border colors for its 3D
  240. widgets, which causes problems when running TK on a system with
  241. "PseudoColor" display class, and a 16-cell colormap.
  242.  
  243. If you can't go to eight bitplanes, you can instead start the server
  244. with a "-static" (Xsco) or "-analog" (Xsight) option, making the
  245. display class become "StaticColor".  This makes the entire colormap
  246. read-only, and it will return the color that most closely maps to the
  247. desired color as possible.
  248.  
  249. This information is from Keith Amann <Keith_Amann@stortek.com>
  250.  
  251. ------------------------------
  252.  
  253. From: -VIII- Questions on building Tcl and friends on your system
  254. Subject: -Q8G- Why do I get format and scan errors when I run tclTest
  255.         (NeXT, AIX, etc.)?
  256.  
  257. A8G. That's a problem (scanf/printf) many systems seem to have.  Don't
  258. worry too much about it - just don't use these 'advanced' features.  If
  259. you're hacking C, you'll have the same problems.
  260.  
  261. ------------------------------
  262.  
  263. From: -VIII- Questions on building Tcl and friends on your system
  264. Subject: -Q8H- Why do I get lots of errors under Irix 4.0.1 when I run
  265.         tclTest?
  266.  
  267. A8H. There's a bug in the 4.0.1 optimizer that's fixed in 4.0.2.
  268. Compile tclVar.c using -O0 (no optimization).
  269.  
  270. ------------------------------
  271.  
  272. From: -VIII- Questions on building Tcl and friends on your system
  273. Subject: -Q8I- Does anyone else have problems with Tcl on a Cray?
  274.  
  275. A8I. See the porting notes for a set of changes mentioned.  Also,
  276. Booker C. Bense <benseb@grumpy.sdsc.edu> reports that version 3.0.1.6
  277. has some real problems with char pointers, causing Tcl to crash.  Using
  278. version 3.0.2.1, things are much better, except for a minor formatting
  279. problem and serious problems with scan.
  280.  
  281. ------------------------------
  282.  
  283. From: -VIII- Questions on building Tcl and friends on your system
  284. Subject: -Q8J- Does anyone know how to get Tk to run on a SparcBook or other
  285.         laptop with a limited number of colors?
  286.  
  287. A8J. On a SparcBook, if you start openwin (the OpenWindows server starting
  288. command) as:
  289.  
  290. openwin -dev "/dev/fb staticvis"
  291.  
  292. you get a static visual color model that Tk copes with better than the
  293. default.  Some things are ugly, but not as ugly as monochrome.
  294.  
  295. ------------------------------
  296.  
  297. From: -VIII- Questions on building Tcl and friends on your system
  298. Subject: -Q8K- What does it take to get Tcl/Tk to compile on 386bsd/Linux 
  299.         or other Posix/ANSI C systems not already supported?
  300.  
  301. A8K. Patches for 386BSD were posted to comp.lang.tcl back in Nov. 1992 to
  302. alt.sources.  See one of the ftp archive sites for this group for them.
  303. Basically, there were some setting of defines and a few places where 
  304. const char * had to be used in place of char *.  
  305.  
  306. ------------------------------
  307.  
  308. From: -VIII- Questions on building Tcl and friends on your system
  309. Subject: -Q8L- Why do I get a parse error in tkInit.c when I try to compile
  310.         Tk on my Irix machine?
  311.  
  312. A8L. Jon Knight <J.P.Knight@loughborough.ac.uk> reports that the version of
  313. Irix that he is using doesn't define a uid_t.  One needs to define one 
  314. somewhere (or comment out the lines causing the problems) to get the compile
  315. to continue.
  316.  
  317. ------------------------------
  318.  
  319. From: -VIII- Questions on building Tcl and friends on your system
  320. Subject: -Q8M- What do I need to do to install Tcl 7.1/Tk 3.4 on NeXTSTEP 3.1?
  321.  
  322. A8M.  I have indicated in the past that this particular question seems
  323. to generate controversy - I have replaced previous explanations with the
  324. latest msg from someone who sounds authoritative...
  325.  
  326. From: Robert Nicholson <robert@steffi.demon.co.uk>
  327.  
  328. This is for TCL7.1 and TK3.4 
  329.  
  330. This is a variation on Thomas Funke's entry.
  331.  
  332. Installing TCL7.1 on NeXT
  333. =========================
  334.  
  335. To install tcl7.1 on NeXTSTEP 3.1 you must:
  336.  
  337. - run configure with predefined CPP:
  338.     type "sh" to run a Bourne shell.
  339.     then type
  340.     CPP='cc -E' ./configure
  341.  
  342. - edit Makefile:
  343.  
  344.   add tmpnam.o to COMPAT_OBJS:
  345.   COMPAT_OBJS =      getcwd.o waitpid.o strtod.o tmpnam.o
  346.  
  347. At this point you probably want to rename the all calls to strtod and
  348. tmpnam to something else in order to distinguish them from those that
  349. are supplied in the NeXT libraries libsys_a.a.  You can do this by 
  350. adding the following line to AC_FLAGS
  351.  
  352.     -Dstrtod=newstrtod -Dtmpnam=newtmpnam
  353.  
  354. and renaming the names of the definitions in the appropriate
  355. compat/*.c files.
  356.  
  357. To check everything is working correctly open a tclsh 
  358.  
  359. % expr {"0" == "+"} 
  360. 0
  361.  
  362. Using the wrong strtod function will cause this test to return 1 which
  363. is a serious error.  Ignore the precision errors.
  364.  
  365. Installing TK3.4 on NEXTSTEP 3.1
  366. ================================
  367.  
  368. There is a call to strtod in TK3.4 so you should link against
  369. ../tcl-7.1/compat/strtod.o and apply the -Dstrtod=newstrtod
  370. to AC_FLAGS in Tk also.
  371.  
  372. Note:Tk's raise test will fail when running the tvtwm window manager.
  373. Changing to either twm or even better fvwm ensures that this test will
  374. succeed. 
  375.  
  376.  
  377.  
  378. ------------------------------
  379.  
  380. From: -VIII- Questions on building Tcl and friends on your system
  381. Subject: -Q8N- Why can't I print the draft of the Tcl/Tk book?
  382.  
  383. A8N.  From ouster@cs.Berkeley.EDU (John Ousterhout):
  384.  
  385. Here's my form letter that seems to explain most of the problems people
  386. have had printing parts of the book:
  387.  
  388. Some old versions of the Transcript spooler software cannot properly
  389. handle encapsulated Postscript files within another Postscript file
  390. when they do page reversal.  They tend to garble the page structure of
  391. the file, causing an error at the point of the first EPS file.  Part
  392. II of the Tcl book has lots of EPS files embedded in it, one for
  393. each screen dump.  If your printer cannot print this file I suggest
  394. checking to see if page reversal is enabled for your printer.  If
  395. so, try asking your local system wizard to disable it for you;  this
  396. should allow the file to print.  Or, find some other way to dump the
  397. Postscript file directly to the printer without going through the
  398. spooling software (e.g. perhaps you can simply cat it to the printer's
  399. serial port).
  400.  
  401. Others have suggested:
  402.  
  403.  The embedded pictures have CR as the line separator instead of LF and
  404.  this may be causing the problem. Try translating them to LFs ...
  405.  
  406.    tr '\015' '\012' <book.p2.ps >fixedbook.p2.ps
  407.  
  408. and:
  409.  
  410.  This is not the original poster's problem but in countries using A4
  411.  paper and on a particular printer, the Dataproducts LZR1260E, the frame
  412.  size causes the printing to be stretched vertically. This happens with
  413.  other Framemaker generated documents by the way. A PS interpreter bug
  414.  no doubt. The fix is to edit the dimensions for A4 paper. This shell
  415.  script does both fixes.
  416.  
  417. #!/bin/sh
  418. cat $1 | tr '\015' '\012' | sed '/FMDOCUMENT$/s/612 792/595 842/'
  419.  
  420. ------------------------------
  421.  
  422. From: -VIII- Questions on building Tcl and friends on your system
  423. Subject: -Q8O- Why am I getting errors on my SGI Indigo workstation?
  424.  
  425. A8O. From Gordon Lack <gml4410@ggr.co.uk> we are told that
  426. the SGI c compiler has some bugs with variable arguments.
  427.  
  428. tclVar.c must be compiled with -O0 at IRIX C 4.0.1 because of a
  429. compiler bug with varargs.
  430.  
  431.    Done by placing specific rule into Makefile.
  432.  
  433. > # GGR SG needs -O0 for varargs at 4.0.1
  434. >
  435. > CC_SWITCHES0 =  -O0 -I. -I${SRC_DIR} ${AC_FLAGS} ${MATH_FLAGS} \
  436. > ${GENERIC_FLAGS} ${PROTO_FLAGS} ${MEM_DEBUG_FLAGS} \
  437. > -DTCL_LIBRARY=\"${TCL_LIBRARY}\"
  438. >
  439. > tclVar.o: tclVar.c
  440. >         $(CC) -c $(CC_SWITCHES0) $<
  441.  
  442.  
  443. ------------------------------
  444.  
  445. From: -IX-  How, in Tcl, can I XXX:
  446. Subject: -Q8P- How do I build expect on Solaris 2.3?
  447.  
  448. A8P. From jra@hrcms.jazz.att.com (Jeff Abramson) we are told that,
  449.  
  450. > using either SunPro cc 2.0.1 or gcc 2.5.8 with
  451. > no problems.  For SunPro I do:
  452. >  
  453. >         CC=cc ./configure --prefix=directory_of_your_choice
  454. >         make CC=cc
  455. >  
  456. > For gcc I do:
  457. >  
  458. >         CC="gcc -fwritable-strings" ./configure \
  459. >        --prefix=directory_of_your_choice
  460. >        make CC="gcc -fwritable-strings"
  461.  
  462.  
  463. ------------------------------
  464.  
  465. From: -IX-  How, in Tcl, can I XXX:
  466. Subject: -Q9A- association lists or property lists?
  467.  
  468. A9A. Use Extended Tcl arrays or keyed lists.
  469.  
  470. For example, if you did a:
  471.  
  472. keylset ttyFields ttyName tty1a
  473. keylset ttyFields baudRate 57600
  474. keylset ttyFields parity strip
  475.  
  476. And then an "echo $ttyFields", you'd get:
  477.  
  478.     {ttyName tty1a} {baudRate 57600} {parity strip}
  479.  
  480. ------------------------------
  481.  
  482. From: -IX-  How, in Tcl, can I XXX:
  483. Subject: -Q9B- call one proc with the multi parameter value returned by 
  484.         another proc?
  485.  
  486. A9B. Assuming y requires multiple args and x returns multiple words, use 
  487.     Tcl's eval command :
  488.  
  489.     eval y [x]
  490.  
  491. ------------------------------
  492.  
  493. From: -IX-  How, in Tcl, can I XXX:
  494. Subject: -Q9C- pass an array into a proc?
  495.  
  496. A9C. Use upvar rather than try to use global variables when possible.  If
  497. the function is event driven, you are forced to use global variables.
  498.  
  499.  
  500. # print elements of an array
  501. proc show_array arrayName {
  502.     upvar $arrayName myArray
  503.  
  504.     foreach element [array names myArray] {
  505.        puts stdout "${arrayName}($element) =  $myArray($element)"
  506.     }
  507. }
  508.  
  509. set arval(0) zero
  510. set arval(1) one
  511. show_array arval
  512.  
  513.  
  514. To return an array from a procedures, just take the array name in as an
  515. argument, as above.  Any changes you make in the array will be made in
  516. the parent's array as well.
  517.  
  518. Extended Tcl introduces a concept called keyed lists which are arrays
  519. made out of lists of key-value pairs and can be passed by value to routines,
  520. over networks, etc.
  521.  
  522. ------------------------------
  523.  
  524. From: -IX-  How, in Tcl, can I XXX:
  525. Subject: -Q9D- pipe output of a command back into a Tcl parsing 
  526.         procedure?
  527.  
  528. A9D. For example, to grep a pattern out of a range of files, one might
  529. do:
  530.  
  531. karl@NeoSoft.com (Karl Lehenbauer) writes:
  532.  
  533. set files [glob /home/cole/stats/*]
  534.  
  535. proc parseInfo { site } {
  536.    global files
  537.  
  538. #
  539. # site is chosen from a listbox earlier
  540. #
  541.    set in [open [concat "|/usr/bin/grep $site $files"] r]
  542.  
  543.    while {[gets $in line]>-1} {
  544.       puts stderr $line
  545.    }
  546.    catch {close $in}
  547. }
  548.  
  549. One thing:  the matching strings are _not_ returned in directory order.
  550.  
  551. But what if I want to check the return code AND use the output of 
  552. the command?  kennykb@dssv01.crd.ge.com (Kevin B. Kenny) writes:
  553.  
  554. if [catch {exec ls} data] {
  555.         # The exec got an error, and $errorCode has its termination status
  556. } else {
  557.         # The exec succeeded
  558. }
  559. # In any case, `data' contains all the output from the child process.
  560.  
  561. Note that Karl Lehenbauer adds that errorCode will be a list containing
  562. three elements, the string "CHILDSTATUS", the process ID of the child,
  563. and the exit status of the child.
  564.  
  565. ------------------------------
  566.  
  567. From: -IX-  How, in Tcl, can I XXX:
  568. Subject: -Q9E- merge extended Tcl into other programs such as wish or expect?
  569.  
  570. A9E. The latest version of extended Tcl, tclX 6.7c, has been enhanced to
  571. make it easier to incorporate into applications.
  572.  
  573. ------------------------------
  574.  
  575. From: -IX-  How, in Tcl, can I XXX:
  576. Subject: -Q9F- delete a procedure from within a script?
  577.  
  578. A9F. rename procedureName ""
  579.  
  580. ------------------------------
  581.  
  582. From: -IX-  How, in Tcl, can I XXX:
  583. Subject: -Q9G- get parray to recognize an array variable I have created
  584.         via upvar?
  585.  
  586. A9G. Right now (June, 1992) upvar doesn't allow you to attach to an
  587. individual element of an array.  This is considered a bug by 
  588. Mr. Ousterhout and has been place on a bug list.
  589.  
  590. ------------------------------
  591.  
  592. From: -IX-  How, in Tcl, can I XXX:
  593. Subject: -Q9H- get more than 7 digits of double precision ?
  594.  
  595. A9H. In Tcl 6.x, modify the tclExpr.c module to use %lf instead of %g.  In
  596. Tcl 7.x, set the global variable tcl_precision instead.
  597.  
  598. ------------------------------
  599.  
  600. From: -IX-  How, in Tcl, can I XXX:
  601. Subject: -Q9I- grab the command line whenever a non-built-in call is made?
  602.  
  603. A9I. The procedure "unknown" is called automatically with arguments 
  604. containing the command and its arguments for any command that couldn't be 
  605. found.  In fact, Tcl and Extended Tcl use this feature to provide demand
  606. loaded commands, and even entire libraries.
  607.  
  608. So by modifying the unknown procedure you can provide your own extended
  609. functionality, or even remove the demand loading capability if you so
  610. desire.
  611.  
  612. ------------------------------
  613.  
  614. From: -IX-  How, in Tcl, can I XXX:
  615. Subject: -Q9J- get or set an environment variable?
  616.  
  617. A9J. By using something like the following.
  618.  
  619. set olddisplay $env(DISPLAY)
  620. set env(DISPLAY) unix:0
  621.  
  622. Thanks to "Joel Fine" <joel@cs.berkeley.edu> for the answer.
  623.  
  624. ------------------------------
  625.  
  626. From: -IX-  How, in Tcl, can I XXX:
  627. Subject: -Q9K- use numbers with leading zeroes?
  628.  
  629. A9K. Dave Morriss was recently having problems because he was trying
  630. to do something like:
  631.  
  632. set index [expr [exec date +%W]%[llength $pop_server_list]]
  633.  
  634. but during the 9th and 9th week of the year, he got errors - 08 and 09
  635. are not valid octal numbers in Tcl.
  636.  
  637. Some of the solutions provided were:
  638.  
  639. From George A. Howlett <george.howlett@att.com>, we got:
  640.  
  641. set wknum [format "%g" [exec date +%W]]
  642. set index [expr [exec $wknum%[llength $pop_server_list]]
  643.  
  644. From Fred Feirtag <feirtag@wave.nrl.navy.mil> :
  645.  
  646. set index [expr (1[exec date +%W]-100)%[llength $pop_server_list]]
  647.  
  648. From Dan R. Schenck <schendr@Texaco.COM>:
  649.  
  650. set index [expr [string trimleft [exec date +%W] 0]%[llength $pop_server_list]]
  651.  
  652. ------------------------------
  653.  
  654. From: -IX-  How, in Tcl, can I XXX:
  655. Subject: -Q9L- find the command line arguments to my application?
  656.  
  657. A9L.  If you are using extended Tcl or Expect, you will find the parameters in
  658. the Tcl variable argv as a list.  Note that in extended Tcl, the name of 
  659. the program is in the Tcl variable programName and NOT in argv[0].  Thanks to
  660. brad@NeoSoft.com (Brad Morrison) and bachww@rtsg.mot.com (Bud Bach) for
  661. this answer.
  662.  
  663. ------------------------------
  664.  
  665. From: -IX-  How, in Tcl, can I XXX:
  666. Subject: -Q9M- put comments in my script, for example in a case statement?
  667.  
  668. A9M. You can't have comments where you have them.  Move the comments inside
  669. of the "{" for the case that you want.  Your code should read:
  670.  
  671. case 1 {
  672.   -1    {
  673.         #
  674.         # Cannot find information sought
  675.         #
  676.         exit 2
  677.         }
  678.    0    {
  679.         #
  680.         # Error in arguments
  681.         #
  682.         exit 1
  683.         }
  684.    default    {
  685.         #
  686.         # Desired information found
  687.         #
  688.         exit 0
  689.         }
  690. }
  691.  
  692. Thanks to gwlester@cpu.com (Gerald W. Lester).
  693.  
  694. ------------------------------
  695.  
  696. From: -IX-  How, in Tcl, can I XXX:
  697. Subject: -Q9N- redirect stdin or stdout safely, including binary data?
  698.  
  699. A9N. With Extended Tcl you can safely do stuff like:
  700.  
  701.         set infp [open "|compress -dc $fileName"]
  702.         set outfp [open "|gzip -c $newFileName" w]
  703.  
  704.         copyfile $infp $outfp
  705.  
  706. Thanks to karl@NeoSoft.com (Karl Lehenbauer) for the code example.
  707.  
  708. ------------------------------
  709.  
  710. From: -IX-  How, in Tcl, can I XXX:
  711. Subject: -Q9O- trap signals, and other more Unix specific functions?
  712.  
  713. A9O. Extended Tcl offers many of these types of functions.  For instance,
  714. extended Tcl has the 'signal' command:
  715.  
  716.         signal action siglist [command]
  717.  
  718.         where action is one of "default", "ignore", "error", "trap", "get",
  719.         plus the POSIX "block" and "unblock" actions (available only on
  720.         POSIX systems, of course).  Siglist is a list of either the symbolic
  721.         or numeric Unix signal (the SIG prefix is optional).  Command is your
  722.         error handler (or a simple {puts stdout "Don't press *that* key!"}  :-)
  723.  
  724. "trap" does what you expect, and I find "error" and "get" to be
  725. extremely useful in interactive programs which demand keyboard
  726. traversal.
  727.  
  728. Extended Tcl also has things like fork, etc.
  729.  
  730. Answer by brad@NeoSoft.com (Brad Morrison).
  731.  
  732. ------------------------------
  733.  
  734. From: -X-  How, in Tcl, can I XXX:
  735. Subject: -Q9P- get quoted strings to work the way I want?
  736.  
  737. A long article dealing with the issues can be found at
  738. ftp://harbor.ecn.purdue.edu/pub/tcl/docs/README.programmer
  739.  
  740. Here are some short answers:
  741.  
  742. Q. I'm trying to build up a command for later execution but am
  743. having trouble with variable values that include whitespace
  744. or special characters.
  745.  
  746. A. The safest way to build up commands is to use the list command
  747. so that you can keep track of the list structure.  Avoid using
  748. double quotes because you can end up with an extra trip through
  749. the evaluator.  We'll illustrate this with a command to create
  750. a button that prints out the label on the button when you click it.
  751.     Wrong answer #1:
  752.     button $myname -text $label -command "puts stdout $label"
  753.     Why? because if $label has whitespace then the puts command will
  754.     be passed the wrong number of arguments.  If $label has $ or [ ]
  755.     characters, they will be interpreted instead of printed.
  756.     Good answer #2:
  757.     button $myname -text $label -command [list puts stdout $label]
  758.     Why? because list will properly quote the value of $label
  759.  
  760. Q. I'm trying to build up a command for later execution but am
  761. having trouble getting some variables to evaluate now, and some
  762. to evaluate later when the command is run.
  763.  
  764. A. The cleanest way to do this is to define a procedure that hides
  765. the use of the variables at run time, and then build up a call to
  766. that procedure using the list command as described previously.  (You
  767. can even define the procedure on the fly.  It will have global scope
  768. even it if is created within another procedure.)
  769.     Wrong answer #1:
  770.     button $myname -text $label -command \
  771.         [list puts stdout $ArrayOfDynamicStuff($label)]
  772.     Why? The array value will be substituted when the button is created,
  773.     not later on when the button is clicked.  Also, note that the
  774.     command is executed at the global scope, so it is not necessary
  775.     to include a "global ArrayOfDynamicStuff" in the command.
  776.     Wrong answer #2 (backquotes and list):
  777.     button $myname -text $label -command \
  778.         [list puts stdout \$ArrayOfDynamicStuff($label)]
  779.     Why? Here the list command and the backquote of $ are fighting with
  780.     each other.  The command ends up being something like:
  781.         puts stdout {$ArrayOfDynamicStuff(foo)}
  782.     which prevents the substitution of the value of the array element.
  783.     Dubious answer #3 (backquotes and double-quotes):
  784.     button $myname -text $label -command \
  785.         "puts stdout \$ArrayOfDynamicStuff($label)"
  786.     Why? This only works if the value of $label has no special characters
  787.     or whitespace.
  788.     Clean answer #4 (proc):
  789.     proc doit { i } {
  790.         global ArrayOfDynamicStuff
  791.         puts stdout $ArrayOfDynamicStuff($i)
  792.     }
  793.     button $myname -text $label -command [list doit $label]
  794.     Why? Using little TCL procs for your button commands is a good habit
  795.     because it eliminates most needs for fancy quoting, and it
  796.     makes it easier to tweak the button command later on.
  797.  
  798. Q. I'm trying to pass along a variable number of args to another procedure
  799. but I'm having trouble getting the $args to expand right.
  800.  
  801. A. Avoid using eval and double quotes because that results in
  802. an extra trip through the interpreter.  The eval command will do
  803. a concat of its arguments if there are more than one, so that
  804. pretty much eliminates the need to group things with double quotes.
  805. Let's extend the button example:
  806.     Wrong answer #1:
  807.     proc mybutton { myname label args } {
  808.         button $myname -text $label -command [list puts stdout $label] $args
  809.     }
  810.     Why? All the extra arguments to mybutton are grouped into one list element
  811.     that is but into the value of $args.  However, the button command
  812.     expects to see individual arguments, not a sub-list.
  813.     Wrong answer #2:
  814.     proc mybutton { myname label args } {
  815.         eval "button $myname -text $label -command [list puts stdout $label] $args"
  816.     }
  817.     Why? The double quotes allow expansion of $label as well as $args, so if
  818.     $label has any whitespace, the button command will be malformed
  819.     Good answer #3:
  820.     proc mybutton { myname label args } {
  821.         set cmd {button $myname -text $label -command [list puts stdout $label]}
  822.         eval $cmd $args
  823.     }
  824.     Why? Eval will first concatenate its two arguments and then run the
  825.     result through the interpreter.  Think of this as stripping off the
  826.     outer curly braces from $cmd and $arg and making a single list
  827.     with all the elements of both.  $label will be evaluated exactly
  828.     once, so the puts command will remain good, and whatever went into
  829.     args will also be processed exactly one time.
  830.  
  831. Q. Why do I get a syntax error in an if/while/for statement?
  832. A. You may have written something like
  833.     wish: set foo bar
  834.     wish: if {$foo == bar} {puts stdout bar}
  835.     syntax error in expression "$foo == bar"
  836.  
  837. in which bar is interpreted as neither a string nor a variable, since
  838. strings as operands in expressions MUST be surrounded by double quotes
  839. or braces. 
  840.  
  841. Change to
  842.     wish: if {$foo == "bar"} {puts stdout bar}
  843. or
  844.     wish: if {$foo == {bar}} {puts stdout bar}
  845.  
  846. always in expressions, depending on if you want expansion performed or
  847. not. 
  848.  
  849. Contributed by "Jesper Blommaskog" <d9jesper@dtek.chalmers.se>
  850.  
  851. ------------------------------
  852.  
  853. From: -X-  How, in Tcl, can I XXX:
  854. Subject: -Q9Q- share procedures between multiple tcl applications?
  855.  
  856. A9Q.  Rather than sourcing the files explicitly, build a tcl library:
  857.  
  858. Step 1.  Put the files in a common directory
  859.  
  860. Step 2.  Build the tclIndex for the "library".  I use a Makefile with
  861. a convention like:
  862.  
  863. install.index:
  864.         (cd ${DESTDIR}/tclscripts/lib; \
  865.         echo 'source /usr/local/lib/tcl/init.tcl;\
  866.      auto_mkindex . *.tk' | tcl ; exit 0)
  867.  
  868. Step 3. Modify your tcl scripts to reference the library:
  869.  
  870. e.g.:
  871.  
  872. # local additions
  873. lappend auto_path /usr/local/lib/tcl_local $env(RDS_TCL_SCRIPTS)/lib
  874.  
  875. Now, as soon as your script tried to reference a procedure in the library,
  876. the "unknown" command autoloads the procedure for you.
  877.  
  878. Contributed by Joe VanAndel <vanandel@ncar.ucar.edu>
  879.  
  880.  
  881. ------------------------------
  882.  
  883. From: -X-  How, in Tk, can I XXX:
  884. Subject: -Q9R- get items inserted into a list?
  885.  
  886. A9R.  A user asked why
  887.  
  888.     linsert $list 0 ..
  889.  
  890. did not result in .. being inserted into list.
  891.  
  892. d9jesper@dtek.chalmers.se (Jesper Blommaskog) replied:
  893.  
  894. When doing list operations other than lappend, you must save the
  895. returned value. This applies to list, lindex, lrange, lreplace at
  896. least.
  897.  
  898. In this example, you would perhaps want to do something like:
  899.  
  900.     set list [ linsert $list 0 .. ] 
  901.  
  902. ------------------------------
  903.  
  904. From: -X-  How, in Tk, can I XXX:
  905. Subject: -Q9S- perform a non-blocking read on a file identifier?
  906.  
  907. A9S.  From Frank Smith <frank@arraysystems.nstn.ns.ca> we are told that
  908. if you have Extended Tcl, you can
  909.  
  910.    read $fileId [fstat $fileId size]
  911.  
  912. This will read only the number of bytes currently available on 'fileId'
  913. and consequently will not block.
  914.  
  915. ------------------------------
  916.  
  917. From: -X-  How, in Tk, can I XXX:
  918. Subject: -Q9T- read and write from a pipe without locking up?
  919.  
  920. A9T. The stdio package has an optimization for speed that buffers characters so 
  921. that you can avoid un-necessary system calls. This isn't very good for 
  922. interactive use so stdio changes it's behaviour if the file is a terminal. When 
  923. you write to a pipe you are not writing to a terminal and so your output is 
  924. buffered, similarly when the program at the other end of the pipe writes its 
  925. response it is also buffered. The end result is the programs at both end of the 
  926. pipe stall waiting for input. It is possible to force the buffers to be written 
  927. using flush in Tcl and fflush() in C, often though, you don't have control over 
  928. the program at the other end of the pipeline and in that case your only 
  929. solution is to use a pseudo-teletype (pty). Unfortunately this isn't all that 
  930. easy.
  931.  
  932. While this isn't directly supported in Tcl the spawn command in expect opens a 
  933. pty and starts a command.
  934.  
  935.  
  936. ------------------------------
  937.  
  938. From: -X-  How, in Tk, can I XXX:
  939. Subject: -Q10.A.1- get my wish application to execute - I just get a
  940.         wish prompt!  Or I just get error msgs about permission
  941.         denied, not found, etc.
  942.  
  943. A10.A.1. Most systems require a full pathname to the interpreter.  
  944. So you cannot start a wish script out as 
  945.  
  946. #! wish -f
  947.  
  948. Likewise, many Unix systems have a maximum length of characters that you can
  949. put on a #! line.  If you exceed this, you do not get the behaviour you
  950. expect.  So do not try to put something like:
  951.  
  952. #! /projects/somethingbig/bin/sun4/wish -f
  953.  
  954. followed by your wish code.  Keep the lines short - under 32 characters is
  955. recommended.
  956.  
  957. Finally, on some machines, white space after the -f causes a problem.  Be
  958. sure that the -f are the last characters on the first line of the file.
  959.  
  960. ------------------------------
  961.  
  962. From: -X-  How, in Tk, can I XXX:
  963. Subject: -Q10.A.2- ,using a machine with less than 8 bit color, run?
  964.  
  965. A10.A.2. Tk doesn't behave very well with less than 8-bit color screens.  To
  966. try to use it, find all the places in the Tk/wish source where 
  967. DefaultDepthOfScreen is invoked to test the number of bit-planes.  Change all
  968. of these to pretend there is just 1 bit-plane, or call a procedure which
  969. monitors a Tcl variable so that it is configurable, and you should be okay.
  970.  
  971. Another alternative is to see if the server you are using has alternative
  972. visual / color models, such as static visual, etc.  One of the alternatives
  973. may allow Tk to work better.
  974.  
  975. Thanks to "Nathaniel Borenstein" <nsb@thumper.bellcore.com> for this info!
  976.  
  977. ------------------------------
  978.  
  979. From: -X-  How, in Tk, can I XXX:
  980. Subject: -Q10.A.3- set X11 resources for a wish application in an 
  981.         app-defaults file?
  982.  
  983. A10.A.3. Read the documentation for the option command.
  984. Then you should consider something like the following - assume the program
  985. name is xwf.
  986.  
  987. The following are two general purpose functions to put into a library:
  988.  
  989. # envVal envValName
  990. #   Looks up the envValName environment variable and returns its
  991. #   value, or {} if it does not exists
  992. proc envVal {envValName} {
  993.   global env
  994.   if [info exists env($envValName)] {return $env($envValName)} {return {}}
  995. }
  996.  
  997. # loadAppDefaults classNameList ?priority?
  998. #   Searches for the app-default files corresponding to classNames in
  999. #   the order specified by X Toolkit Intrinsics, and loads them with
  1000. #   the priority specified (default: startupFile).
  1001. proc loadAppDefaults {classNameList {priority startupFile}} {
  1002.   set filepath "[split [envVal XUSERFILESEARCHPATH] :] \
  1003.         [envVal XAPPLRESDIR] \
  1004.         [split [envVal XFILESEARCHPATH] :] \
  1005.         /usr/lib/X11"
  1006.   foreach i $classNameList {
  1007.     foreach j $filepath {
  1008.       if {[file exists $j/$i]} {
  1009.     option readfile $j/$i $priority; break
  1010.       }
  1011.     }
  1012.   }
  1013. }
  1014.  
  1015. # Now, here is what you would put into xwf:
  1016.  
  1017. option add Tk.BoldFont "*-lucida sans-Bold-R-Normal-*-100-*" widgetDefault
  1018. loadAppDefaults {xwf XWF} userDefault
  1019.  
  1020. This sets a program default, then load any defaults specified in the user's
  1021. default resources and finally any site or general app-defaults resource.  
  1022. Of course, you would want to add some xwf command line handling to allow 
  1023. the user to override things at execution time.
  1024.  
  1025. ------------------------------
  1026.  
  1027. From: -X-  How, in Tk, can I XXX:
  1028. Subject: -Q10.A.4- specify bitmap patterns on the command line instead of 
  1029.         just as a file name?
  1030.  
  1031. A10.A.4. You can not, at least as of June, 1992.
  1032.  
  1033. ------------------------------
  1034.  
  1035. From: -X-  How, in Tk, can I XXX:
  1036. Subject: -Q10.A.5- get a Motif user interface?
  1037.  
  1038. A10.A.5. Tk does not currently use the Xt toolkit, so a strict adherence
  1039. to Motif via the libXm.a routines is not possible.  However, the authors
  1040. of Tk prefer the Motif style of user interface, so you will find that Tk
  1041. makes quite an attempt to implement a Motif-like interface.
  1042.  
  1043. ------------------------------
  1044.  
  1045. From: -X-  How, in Tk, can I XXX:
  1046. Subject: -Q10.A.6- get an OpenLook user interface?
  1047.  
  1048. A10.A.6. Unfortunately, Tk does not currently use either XView or Xt based
  1049. widgets in its user interface, so an OpenLook compliant (or even
  1050. similar) interface is probably not easily achievable in the near future.
  1051.  
  1052. ------------------------------
  1053.  
  1054. From: -X-  How, in Tk, can I XXX:
  1055. Subject: -Q10.A.7- get Tk 3.3 to even start - I get security error messages.
  1056.  
  1057. A10.A.7.  Alternate forms of this question often mention that Tk 3.3 send
  1058. is broken, or ask how to use xauth.
  1059.  
  1060. Under Tk 3.3, the X11 xauth security mechanism is used.  While this
  1061. provides more security, it does require the user to do a bit more
  1062. setup.  The user needs to create an Xauthority file (typically
  1063. $HOME/.Xauthority) and then restart the X server with the
  1064. -auth argument, along with the name of the Xauthority file created.
  1065. Read the X11 documentation for your system for the details on how to
  1066. use xauth or comparable software to create the authority files needed.
  1067. An intro to xauth is available as
  1068. ftp://harbor.ecn.purdue.edu/pub/tcl/docs/Xauthority .
  1069.  
  1070. Also note that you can configure Tk to not depend on xauth by modifying
  1071. the tk3.3/Makefile.in to comment out the following:
  1072.  
  1073. # To turn off the security checks that disallow incoming sends when
  1074. # the X server appears to be insecure, reverse the comments on the
  1075. # following lines:
  1076. #SECURITY_FLAGS =
  1077. SECURITY_FLAGS = -DTK_NO_SECURITY
  1078.  
  1079. ------------------------------
  1080.  
  1081. From: -X-  How, in Tk, can I XXX:
  1082. Subject: -Q10.B.1- change the default class bindings?
  1083.  
  1084. A10.B.1. All default class bindings for Tk widgets are initialized in
  1085. $tk_library/tk.tcl.  Use this file as a guide to implement new
  1086. bindings.  For instance, the following code duplicates Button 1's
  1087. drag-select facility in Button 3 for all listboxes:
  1088.  
  1089. bind Listbox <3> {%W select from [%W nearest %y]}
  1090. bind Listbox <B3-Motion> {%W select to [%W nearest %y]}
  1091.  
  1092. ------------------------------
  1093.  
  1094. From: -X-  How, in Tk, can I XXX:
  1095. Subject: -Q10.B.2- delete a binding?
  1096.  
  1097. A10.B.2. Give an empty-string command to the "bind" invocation.  For
  1098. example, to disable the Delete key in all entry fields:
  1099.  
  1100.     bind Entry <Delete> {}
  1101.  
  1102. ------------------------------
  1103.  
  1104. From: -X-  How, in Tk, can I XXX:
  1105. Subject: -Q10.B.3- change a binding while it is being executed?
  1106.  
  1107. A10.B.3. As of June, 1992, this was not a safe thing to do in Tk.  It was
  1108. put on the bug list by John Ousterhout to be fixed in a future version.
  1109.  
  1110. The solution for now is not to change the bindings, but to change
  1111. something in the code they execute.  For example, keep a state variable
  1112. that indicates which binding you'd like, but always have the binding
  1113. call a given procedure.  Then that procedure checks the variable and
  1114. executes one piece of code or another.  Or, you could just make the
  1115. binding's command "eval $cmd" and then change the variable "cmd"
  1116. depending on your application's state.
  1117.  
  1118. ------------------------------
  1119.  
  1120. From: -X-  How, in Tk, can I XXX:
  1121. Subject: -Q10.B.4- bind the arrow key on my Sun keyboard?
  1122.  
  1123. A10.B.4. You have to call it <Left> rather than <R10>.  Under X11, keys are
  1124. referred to by their keysym.  One can use either xmodmap -pk or the xev
  1125. program to determine what the keysym a particular key on a keyboard is
  1126. currently generating.
  1127.  
  1128. If the keysym that is being used is not known by Tk, you may have to edit
  1129. its ks_names.h file.  There is a note in this file that indicates that
  1130. one should not edit it - but this is where the keysym must be for it to
  1131. be recognized.
  1132.  
  1133. Thanks to Wayne Christopher <faustus@CS.Berkeley.EDU> for this
  1134. note.
  1135.  
  1136. ------------------------------
  1137.  
  1138. From: -X-  How, in Tk, can I XXX:
  1139. Subject: -Q10.B.5- get root's mouse bindings to work in my Tk application?
  1140.  
  1141. A10.B.5. Some window managers, such as mwm, define mouse button bindings
  1142. which cause Tk some problems.  Try saving off the window manager's startup
  1143. file (something like /.mwmrc for instance) and then copy in a startup
  1144. file from a login id that works.  Thanks to brad@NeoSoft.com (Brad Morrison)
  1145. for this invaluable tip!
  1146.  
  1147. ------------------------------
  1148.  
  1149. From: -X-  How, in Tk, can I XXX:
  1150. Subject: -Q10.C.1- get an application to also use libXt?
  1151.  
  1152. A10.C.1. Tk2.1 and Xt have different X connections, and XtAppNextEvent will
  1153. block is there is nothing coming from the X connection.  One way
  1154. of fixing this is get the connection number of Tk using
  1155.  
  1156.    ConnectionNumber(Tk_Display(tk_window));
  1157.  
  1158. and using XtAddInput to register this with the Xt event handler.  The
  1159. callback procedure for XtAddInput wrapper procedure that runs
  1160. Tk_OneEvent(1).  There might be problems with Tk file sources which
  1161. aren't registered with Xt.
  1162.  
  1163. Thanks to joe@astro.as.utexas.edu (Joe Wang) for this information.
  1164.  
  1165. ------------------------------
  1166.  
  1167. From: -X-  How, in Tk, can I XXX:
  1168. Subject: -Q10.C.2- change the X11 cursor?
  1169.  
  1170. A10.C.2. Here is a tip from mgc@cray.com (M. G. Christenson).
  1171.  
  1172. Look at /usr/include/X11/cursorfont.h for a list of available cursors.
  1173. You can use the names in there by removing the 'XC_'.  
  1174.  
  1175. Here's a little proc I use to make my entire application go 'busy'
  1176. while it's doing something. Just call it with the commands you want to
  1177. execute, and the watch cursor will be displayed for the time it takes
  1178. the commands to complete.  Note that any new windows will have their
  1179. normal cursor.
  1180.  
  1181. proc busy {cmds} {
  1182.     global errorInfo
  1183.  
  1184.     set busy {.app .root}
  1185.     set list [winfo children .]
  1186.     while {$list != ""} {
  1187.     set next {}
  1188.     foreach w $list {
  1189.         set class [winfo class $w]
  1190.         set cursor [lindex [$w config -cursor] 4]
  1191.         if {[winfo toplevel $w] == $w || $cursor != ""} {
  1192.         lappend busy [list $w $cursor]
  1193.         }
  1194.         set next [concat $next [winfo children $w]]
  1195.     }
  1196.     set list $next
  1197.     }
  1198.  
  1199.     foreach w $busy {
  1200.     catch {[lindex $w 0] config -cursor watch}
  1201.     }
  1202.  
  1203.     update idletasks
  1204.  
  1205.     set error [catch {uplevel eval [list $cmds]} result]
  1206.     set ei $errorInfo
  1207.  
  1208.     foreach w $busy {
  1209.     catch {[lindex $w 0] config -cursor [lindex $w 1]}
  1210.     }
  1211.  
  1212.     if $error {
  1213.     error $result $ei
  1214.     } else {
  1215.     return $result
  1216.     }
  1217. }
  1218.  
  1219. ------------------------------
  1220.  
  1221. From: -X-  How, in Tk, can I XXX:
  1222. Subject: -Q10.C.3- raise or lower a window?
  1223.  
  1224. A10.C.3. This is on the (semi-infinite) list of things to be done in the future.
  1225. If you have the time, please go ahead and add it and submit the code and all
  1226. will be grateful.
  1227.  
  1228. ------------------------------
  1229.  
  1230. From: -X-  How, in Tk, can I XXX:
  1231. Subject: -Q10.C.4- re-map a withdrawn window id?
  1232.  
  1233. A10.C.4. Use wm deiconify <windowid>.
  1234.  
  1235. ------------------------------
  1236.  
  1237. From: -X-  How, in Tk, can I XXX:
  1238. Subject: -Q10.C.5- use Tk in a subwindow of a non-Tk X11 application?
  1239.  
  1240. A10.C.5. From faustus@CS.Berkeley.EDU (Wayne A. Christopher):
  1241.  
  1242. [C]reate the Tk toplevel window but don't map it (wm withdraw).  Then
  1243. re-parent the window to be a subwindow of your other one and then map
  1244. it.  I have done this when the Tk application is a separate process,
  1245. but if it's the same process I think you will get into trouble with the
  1246. event loop, since each toolkit wants control.
  1247.  
  1248. ------------------------------
  1249.  
  1250. From: -X-  How, in Tk, can I XXX:
  1251. Subject: -Q10.C.6- bind <Enter> and <Leave> to a frame containing 
  1252.     other widgets?
  1253.  
  1254. A10.C.6. From John Ousterhout, ouster@cs.Berkeley.EDU):
  1255.  
  1256. Each <Enter> or <Leave> event has a "detail" field, which you can access
  1257. from bindings with "%d".  If the detail is "NotifyInferior" it means the
  1258. pointer has moved into or out of a child window (so it's really still in
  1259. the area of the window receiving the event).  You should be able to check
  1260. the detail in your scripts and ignore events with a detail of NotifyInferior.
  1261. Check the Xlib documentation for Enter/Leave events for complete
  1262. information on the detail field.
  1263.  
  1264.  
  1265. ------------------------------
  1266.  
  1267. From: -X-  How, in Tk, can I XXX:
  1268. Subject: -Q10.C.7- mix interactions between Xt and Tk/Tcl?
  1269.  
  1270. A10.C.7.
  1271.  
  1272. [From YIP Chi Lap [Beta] <h9118101@hkuxa.hku.hk> ]
  1273.     I have come up with a CPU hogging polling loop that
  1274. works (till now) for my application which not much
  1275. direct interaction between a Tk window and an Xt window is made.
  1276. (e.g., it won't draw something on a Tk window by a Xt-dispatched routine)
  1277.  
  1278.   XEvent xevent;
  1279.   for (;;)
  1280.   {
  1281.     if (XtAppPending(appcontext))
  1282.     {
  1283.       XtAppNextEvent(appcontext,&xevent);
  1284.       XtDispatchEvent(&xevent);
  1285.     }
  1286.     Tk_DoOneEvent(TK_DONT_WAIT);
  1287.   }
  1288.  
  1289. [ From David C Mudie <mudie@radon.eecs.berkeley.edu> ]
  1290.     We use the main event loop below.  The basic idea
  1291. is to watch for events arriving from either the Xt server
  1292. connection or the Tk server connection and then call
  1293. the library dispatchers.
  1294.  
  1295.     The code fragment below will need to be cleaned up for
  1296. your usage; there are some missing include statements
  1297. and global declarations buried elsewhere in the file.
  1298.  
  1299. /* Allow Tcl/Tk and Xt to work at the same time. */
  1300. void tkGo(void)
  1301. {
  1302.   extern Widget topLevel;
  1303.   int width = ulimit(4, -1);
  1304.   fd_set readfds;
  1305.   struct timeval timeout;
  1306.   int nfds;
  1307.   int tkfd = ConnectionNumber(Tk_Display(mainWindow));
  1308.   int xtfd = ConnectionNumber(XtDisplay(topLevel));
  1309.  
  1310.   Tcl_VarEval(interp, "update", NULL);
  1311.   while (1) {
  1312.  
  1313.     /* Select on X server connections to wait for event. */
  1314.     /* Timeout every half second to allow processing of non-X events */
  1315.  
  1316.     FD_ZERO(&readfds);
  1317.     FD_SET(tkfd, &readfds);
  1318.     FD_SET(xtfd, &readfds);
  1319.     timeout.tv_sec = 0;
  1320.     timeout.tv_usec = 500000;
  1321.  
  1322.     nfds = select(width, &readfds, NULL, NULL, &timeout);
  1323.     if (nfds < 0 && errno != EINTR) {
  1324.       perror("select");
  1325.     } else if (nfds == 0) {
  1326.       /* printf("timeout\n"); */
  1327.     }
  1328.  
  1329.     while (XtPending()) {
  1330.       XtProcessEvent(XtIMAll);
  1331.     }
  1332.  
  1333.     while (Tk_DoOneEvent(1)) {
  1334.       /* do nothing */
  1335.     }
  1336.   
  1337.     if (mainWindow == NULL) {
  1338. //      Tcl_DeleteInterp(interp);
  1339. //      Tcl_DStringFree(&command);
  1340.       Tcl_Eval(interp, "exit");
  1341.       return;
  1342.     }
  1343.   }
  1344. }
  1345.  
  1346. [ From Davide Frisoni <frisoni@faw.uni-ulm.de> ]
  1347.     One may use Xt properties to communicate between different
  1348. applications.
  1349.  
  1350.  
  1351. ------------------------------
  1352.  
  1353. From: -X-  How, in Tk, can I XXX:
  1354. Subject: -Q10.D.1- resize a listbox?
  1355.  
  1356. A10.D.1. Use wm min/maxsize - in a uniform manner.  Here is a resizable listbox:
  1357.  
  1358.         #!/usr/local/bin/wish -f
  1359.         wm minsize . 20 20
  1360.         wm maxsize . 1152 900
  1361.         pack append . [listbox .l -borderwidth 2 -relief raised] {expand fill}
  1362.  
  1363. Doing the same with the text widget brings its resizing under control too.
  1364.  
  1365. Thanks to "John C Ellson" <ellson@ontap.att.com).
  1366.  
  1367. ------------------------------
  1368.  
  1369. From: -X-  How, in Tk, can I XXX:
  1370. Subject: -Q10.D.2- select two items that are not adjacent in the listbox at 
  1371.         one time?
  1372.  
  1373. A10.D.2.  See Marc R. Ewing's Listbox.patch for a way to modify Tk to allow
  1374. selection of non-contiguous entries.
  1375.  
  1376. ------------------------------
  1377.  
  1378. From: -X-  How, in Tk, can I XXX:
  1379. Subject: -Q10.D.3- select items in more than one Tk listbox at a time?
  1380.  
  1381. A10.D.3. The default for Tk's listbox widget exports its selection as the
  1382. X selection.  There can only be one of these at a time.
  1383.  
  1384. To turn off this behavior in Tk, use the -exportselection false when
  1385. you create the listbox.  Or, use the
  1386.  
  1387. option add *Listbox.exportselection false
  1388.  
  1389. command in the beginning of your script.
  1390.  
  1391. Thanks to David Herron <david@davids.mmdf.com> for this tip.
  1392.  
  1393. ------------------------------
  1394.  
  1395. From: -X-  How, in Tk, can I XXX:
  1396. Subject: -Q10.D.4- avoid fractional white space at the end of a resizable
  1397.         listbox?
  1398.  
  1399. A10.D.4. First, let's state the problem more clearly. I want to make a
  1400. resizable listbox and I don't want to constrain it by setting a
  1401. minimum size. So I pack it with fill expand, I use it to set the grid
  1402. (so that resizing the window always gives me whole lines) and I set an
  1403. initial geometry of 1x1 to override the default. However what I get
  1404. is... (run the code below for a demo and try to resize)
  1405.  
  1406. listbox .l -geom 1x1 -setgrid 1 -yscrollcommand ".s set" -relief sunken -bd 2
  1407. scrollbar .s -command ".l yview"
  1408. pack .s -side right -fill y
  1409. pack .l -side top -fill both -expand 1
  1410. .l insert end one two three four five six seven eight nine ten "THE END"
  1411.  
  1412. ...a situation where the partially filled listbox has a blank
  1413. half-line at the bottom even if there are more items in the list. This
  1414. is very confusing because it fools the user into thinking that there
  1415. is nothing else beyond what's visible. Why does it happen?
  1416.  
  1417. Now for the answer.
  1418.     Look at the window as it is created, before the resizing. See that
  1419. poor, tiny little scrollbar squeezed in that microscopic window? It,
  1420. too, requests a minimum size, and it so happens that the starting
  1421. geometry for the listbox (i.e. the situation referred to as "1x1")
  1422. receives some free fractional space at the bottom. And you never get
  1423. rid of it, since the resizing is constrained to be in whole
  1424. characters.
  1425.  
  1426. The thing to do is to define the initial layout in such a way that the
  1427. widget that has -setgrid actually displays an integer number of lines
  1428. and columns. In the above example, setting -geom 1x2 does the trick.
  1429.  
  1430. Thanks to Frank Stajano <fms@cam-orl.co.uk> for this tip.
  1431.  
  1432. ------------------------------
  1433.  
  1434. From: -X-  How, in Tk, can I XXX:
  1435. Subject: -Q10.E.1- get output from a Tk canvas?
  1436.  
  1437. A10.E.1. The latest Tk has a save suboption on canvas which allows one
  1438. to create a file describing the canvas.  The default output is Encapsulated
  1439. Postscript, but there is an xpm3 suboption as well.
  1440.  
  1441. ------------------------------
  1442.  
  1443. From: -X-  How, in Tk, can I XXX:
  1444. Subject: -Q10.E.2- fill a canvas which is bounded by lines as opposed to a
  1445.         shape like a polygon, oval, etc.?
  1446.  
  1447. A10.E.2. No, you have to at least use a polygon if you want to fill an area
  1448. bounded by some lines.
  1449.  
  1450. ------------------------------
  1451.  
  1452. From: -X-  How, in Tk, can I XXX:
  1453. Subject: -Q10.E.3- raise/lower canvas window objects or draw graphics onto a 
  1454.         window object inside a canvas?
  1455.  
  1456. A10.E.3. You can't yet. 
  1457.  
  1458. "Jesper Blommaskog" <d9jesper@dtek.chalmers.se>.
  1459.  
  1460. ------------------------------
  1461.  
  1462. From: -X-  How, in Tk, can I XXX:
  1463. Subject: -Q10.E.4- detect when the canvas has been resized?
  1464.  
  1465. A10.E.4. From Nathaniel Pryce <np@hpl.hp.co.uk>:
  1466.  
  1467. > I have a window with a canvas containing some stuff that logically
  1468. > fills the canvas (say, a chess board or a map of the world). I want
  1469. > this toplevel window to be resizable and of course, when the win is
  1470. > resized, I want the stuff inside the canvas to be scaled
  1471. > accordingly. Now, I can manage the scaling of the stuff, but what
  1472. > I'm having trouble with is DETECTING that I should do it. How can I
  1473. > be notified that the window has been resized?
  1474.  
  1475. Nat's answer is:
  1476.  
  1477. You need to bind a command to the Configure event, like this:
  1478.  
  1479.     proc config {w h} {
  1480.        puts stdout ".canvas - width = $w, height = $h"
  1481.     }
  1482.  
  1483.     bind .canvas <Configure> "config %w %h"
  1484.  
  1485.  
  1486. .canvas - width = 224, height = 251
  1487. .canvas - width = 224, height = 151
  1488. .canvas - width = 224, height = 243
  1489. # and so forth
  1490.  
  1491. ------------------------------
  1492.  
  1493. From: -X-  How, in Tk, can I XXX:
  1494. Subject: -Q10.F.1- get the name of my own interpreter?
  1495.  
  1496. A10.F.1. george.howlett@att.com (George A. Howlett) points us to the winfo manual
  1497. page - winfo name . gets the name of the current application.
  1498.  
  1499. ------------------------------
  1500.  
  1501. From: -X-  How, in Tk, can I XXX:
  1502. Subject: -Q10.F.2- get -relief to work on my text widgets?
  1503.  
  1504. A10.F.2. From Owen Rees <rtor@ansa.co.uk>, we find out that we must:
  1505.  
  1506. "[m]ake the border width non-zero as in"
  1507.  
  1508.    text .t -width 20 -height 20 -relief sunken -borderwidth 4
  1509.  
  1510. ------------------------------
  1511.  
  1512. From: -X-  How, in Tk, can I XXX:
  1513. Subject: -Q10.F.3- create a scrollable window of buttons?
  1514.  
  1515. A10.F.3. There are at least two ways to do this.  First, there is a hypertext
  1516. widget that one can get from the Tcl User Contributed Code Archive -
  1517. (see "tcl-faq/part4") and (see "tcl-faq/part5") for details -
  1518.  which provides such a facility.
  1519.  
  1520. And here is some sample code from 
  1521. "Michael Moore" <mdm@stegosaur.cis.ohio-state.edu> which shows a way to 
  1522. do this using just Tk.
  1523.  
  1524. #! /bin/wish -f
  1525. #
  1526. # This demonstrates how to create a scrollable canvas with multiple
  1527. # buttons.
  1528. #
  1529. # Author : Michael Moore
  1530. # Date   : November 17, 1992
  1531. #
  1532.  
  1533. #
  1534. # This procedure obtains all the items with the tag "active"
  1535. # and prints out their ids.
  1536.  
  1537. proc multi_action {} {
  1538.     set list [.frame.canvas find withtag "active"]
  1539.     puts stdout "Active Item Ids : "
  1540.     foreach item $list {
  1541.     puts stdout $item
  1542.     }
  1543. }
  1544.  
  1545. # This simulates the toggling of a command button...
  1546. # Note that it only works on a color display as is right now
  1547. # but the principle is the same for b&w screens.
  1548. proc multi_activate {num id} {
  1549.     
  1550.     set tags [.frame.canvas gettags $id]
  1551.     if {[lsearch $tags "active"] != -1} {
  1552.     .frame.canvas dtag $id "active"
  1553.     .frame.canvas.button$num configure \
  1554.         -background "#060" \
  1555.         -activebackground "#080" 
  1556.     } else {
  1557.     .frame.canvas addtag "active" withtag $id
  1558.     .frame.canvas.button$num configure \
  1559.         -background "#600" \
  1560.         -activebackground "#800"
  1561.     }
  1562.  
  1563. proc setup {} {
  1564.      frame .frame
  1565.  
  1566.      scrollbar .frame.scroll \
  1567.          -command ".frame.canvas yview" \
  1568.          -relief raised
  1569.  
  1570.      canvas .frame.canvas \
  1571.          -yscroll ".frame.scroll set" \
  1572.          -scrollregion {0 0 0 650} \
  1573.          -relief raised \
  1574.      -confine false \
  1575.      -scrollincrement 25
  1576.  
  1577.      pack append .frame \
  1578.          .frame.scroll    {left frame center filly} \
  1579.          .frame.canvas    {left frame center fillx filly}
  1580.  
  1581.      pack append .\
  1582.          .frame   {left frame center fillx filly}
  1583.  
  1584.      button .frame.canvas.action  \
  1585.          -relief raised \
  1586.          -text "Action" \
  1587.      -command "multi_action"
  1588.      .frame.canvas create window 1 25 \
  1589.      -anchor w \
  1590.          -window .frame.canvas.action
  1591.      for {set i 2} {$i < 26} {incr i} {
  1592.      button .frame.canvas.button$i  \
  1593.         -relief raised \
  1594.         -background "#060" \
  1595.         -foreground wheat \
  1596.         -activebackground "#080" \
  1597.         -activeforeground wheat \
  1598.         -text "Button $i" 
  1599.      set id [.frame.canvas create window 1 [expr $i*25] \
  1600.         -anchor w \
  1601.         -window .frame.canvas.button$i]
  1602.      .frame.canvas.button$i configure \
  1603.         -command "multi_activate $i $id"
  1604.     }
  1605. }
  1606.  
  1607. setup
  1608.  
  1609. ------------------------------
  1610.  
  1611. From: -X-  How, in Tk, can I XXX:
  1612. Subject: -Q10.F.4- pack a text widget so that it can be resized interactively?
  1613.  
  1614. A10.F.4. From Spencer W. Thomas <spencer@med.umich.edu> we find that we need to:
  1615.  
  1616. wm minsize . 0 0
  1617. text .text
  1618. pack append . .text {fill expand}
  1619.  
  1620. ------------------------------
  1621.  
  1622. From: -X-  How, in Tk, can I XXX:
  1623. Subject: -Q10.F.5- create a widget with an upper case name?
  1624.  
  1625. A10.F.5. During a recent revision of Tk, things were changed so that names
  1626. beginning with a capital letter are reserved for class names.  Specific
  1627. instances of widgets must begin with a lower case letter.  This enables
  1628. X11 resource definitions to distinguish between a class and instance.
  1629.  
  1630. ------------------------------
  1631.  
  1632. From: -X-  How, in Tk, can I XXX:
  1633. Subject: -Q10.F.6- create equal sized buttons?
  1634.  
  1635. A10.F.6. Recently, Micael Salmon <etxmesa@eos.ericsson.se> posted:
  1636.  
  1637. In article <1993Jun23.065417.4302@ericsson.se>, I write:
  1638. |> I am currently working on yet another tn3270 emulator and I have run
  1639. |> into a problem with creating equal sized buttons. I have created an
  1640. |> array of buttons for PF and cursor keys and I would now like to make
  1641. |> them all the same size. Arranging them into columns was not
  1642. |> a problem but when I add bitmaps the buttons lose their horizontal
  1643. |> alignment. What I tried was to read the height and width of the buttons
  1644. |> using winfo, determine the largest and then use pads to force them to
  1645. |> be the same size, this doesn't seem to work. The technique of using
  1646. |> pre-set height and width doesn't seem applicable when using a mixture
  1647. |> of bitmaps and text as the size in pixels of a text button is font
  1648. |> dependent. All suggestions welcome.
  1649.  
  1650. Jim Wight <J.K.Wight@newcastle.ac.uk> suggested using reqheight and reqwidth
  1651. and then specifying padx and pady in the pack command for each button.
  1652. Jim says:
  1653. I think it only fair to point out that it was Tuomas J Lukka
  1654. <lukka@klaava.Helsinki.FI> who suggested the use of reqwidth and reqheight when I asked how to get over the deficiencies in my first attempt at a solution
  1655. that I posted to this group.
  1656.  
  1657. Since you're all agog I might as well post the final solution (more or less)
  1658. that I mailed to Michael.
  1659.  
  1660. frame .frame1
  1661. frame .frame2
  1662. button .frame1.a -text "pretty long button text"
  1663. button .frame1.b -text "short one"
  1664. button .frame2.c -bitmap "@/usr/include/X11/bitmaps/xlogo32"
  1665. button .frame2.d -text "tiny"
  1666.  
  1667. set long [winfo reqwidth .frame1.a]
  1668. set short [winfo reqwidth .frame1.b]
  1669. set medium [winfo reqwidth .frame2.c]
  1670. set tiny [winfo reqwidth .frame2.d]
  1671. set pady [expr [winfo reqheight .frame2.c]-[winfo reqheight .frame2.d]]
  1672.  
  1673. pack append .frame1 .frame1.a "filly pady $pady"
  1674. pack append .frame1 .frame1.b "fillx padx [expr $long-$short] filly pady $pady"pack append .frame2 .frame2.c "fillx padx [expr $long-$medium] filly"
  1675. pack append .frame2 .frame2.d "fillx padx [expr $long-$tiny] filly pady $pady"
  1676. pack append . .frame1 {left} .frame2 {left}
  1677.  
  1678.  
  1679. ------------------------------
  1680.  
  1681. From: -X-  How, in Tk, can I XXX:
  1682. Subject: -Q10.F.7- vertically stack radio buttons aligning regardless of font?
  1683.  
  1684. A10.F.7.  Till Brychcy <brychcy@informatik.tu-muenchen.de> provides
  1685. the following example:
  1686.  
  1687.  
  1688.     radiobutton .times -text Times -anchor w
  1689.     radiobutton .helvetica -text Helvetica -anchor w
  1690.     radiobutton .courier -text Courier -anchor w
  1691.     pack .times .helvetica .courier -side top -fill x
  1692.  
  1693. Note that you are using anchor west in the widgets themselves, and not in
  1694. the packer.  This lets the packer produce full width buttons.
  1695.  
  1696.  
  1697. ------------------------------
  1698.  
  1699. From: -X-  How, in Tk, can I XXX:
  1700. Subject: -Q10.F.8- initialize an entry widget with some text?
  1701.  
  1702. A10.F.8.  kesch@kappco.kapp-coburg.de (Peter Kesch) provides us with
  1703. one example:
  1704.  
  1705. set default "foobar"
  1706. entry .foo -width 25 -state disabled -textvariable default
  1707.  
  1708.  
  1709. ------------------------------
  1710.  
  1711.  
  1712. End of comp.lang.tcl Frequently Asked Questions (2/5)
  1713. *****************************************************
  1714.